package gjbsh.bangong.ds.controller;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import gjbsh.bangong.ds.model.用户;
import gjbsh.bangong.ds.service.MailService;
import gjbsh.bangong.ds.service.用户Service;
import gjbsh.support.DESCryption;
import gjbsh.support.JsonResult;

/**
 * @author yaolongjun
 * @date   2020年5月21日 上午10:34:35
 */
@RestController
@RequestMapping("user")
public class 用户Controller {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	用户Service ddService;

	@Autowired
	MailService ddMailService;

	// 单位找回密码--根据单位名称、用户名和密码查询是否存在此用户，若存在把用户名和密码发到邮箱
	@RequestMapping("find_by_deptname_and_username_and_email")
	public JsonResult<?> find_by_deptname_and_username_and_email(
			@RequestParam("deptname")String ppDeptName,
			@RequestParam("username")String ppUserName,
			@RequestParam("email")String ppEmail){
		try {
			ppDeptName = ppDeptName.trim();
			ppUserName = ppUserName.trim();
			ppEmail = ppEmail.trim();

			if("".equals(ppDeptName)) {
				return JsonResult.getErrorResult("单位名称不能为空");
			}
			if("".equals(ppUserName)) {
				return JsonResult.getErrorResult("用户名不能为空");
			}
			if("".equals(ppEmail)) {
				return JsonResult.getErrorResult("电子邮箱不能为空");
			}

			// 根据单位名称、用户名和密码查询是否存在此用户，若不存在或存在多条则报错，存在唯一一条发送邮件
			List<Map<String,Object>> mmUserList = ddService.findByDeptnameAndUsernameAndEmail(ppDeptName,ppUserName,ppEmail);
			if(mmUserList.size() == 0) {
				return JsonResult.getErrorResult("根据您提交的信息未查询到用户名、密码，请核实后再提交！");
			}

			if(mmUserList.size() > 1) {
				return JsonResult.getErrorResult("根据您提交的信息查询到多条用户名、密码，请联系管理员核实！");
			}

			Map<String,Object> mmUserMap = mmUserList.get(0);
			String mmPassword = mmUserMap.get("登录密码") == null ? "" : mmUserMap.get("登录密码").toString();
			// 密码解密
			try {
				DESCryption mmDESCryption = new DESCryption();
				mmPassword = mmDESCryption.decode(mmPassword);	
			}catch(Exception e) {				
				mmPassword = mmPassword;
			}

			String mmMailSubject = "中国博士后网上办公系统密码找回";
			String mmMailContent = "尊敬的 "+ ppDeptName + " 管理员，您好！您的登录账号为："+ ppUserName + "，密码为：" + mmPassword + "。";

			// 发送邮件
			ddMailService.sendSimpleMail(ppEmail, mmMailSubject, mmMailContent);

			return JsonResult.getSuccessResult("发送成功！");
		} catch (Exception e) {
			logger.error("用户Controller->find_by_deptname_and_username_and_email: "+e.getMessage());
			return JsonResult.getErrorResult("根据单位名称、用户名称和电子邮箱查找信息失败");
		}
	}

	@RequestMapping("resetpwd")
	public JsonResult<?> resetpwd(){
				try {
		
			List<用户> mmList = ddService.findAllYonghu();
			for(int i=0;i<mmList.size();i++){
				用户 mm用户 = mmList.get(i);
				String mmPwd = Decode(mm用户.get登录密码());
				
				if(Detectpassword(mmPwd)) {
					mmPwd = "bsh_"+ mmPwd;

					DESCryption des;
					des = new DESCryption();
					mm用户.set登录密码(des.encode(mmPwd));
					ddService.modify(mm用户);
				}
				
			}

			return JsonResult.getSuccessResult("重置成功");
		} catch (Exception e) {
			logger.error("用户Controller -> resetpqd: "+e.getMessage());
			return JsonResult.getErrorResult("重置失败");
		}
	}

	//public static final String password = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{8,}$";

	public static final String password1 = "^[-\\+]?[\\d]*$";
	
	public static boolean Detectpassword(String str) {

		boolean flag = false;

		if(str.matches(password1)){
			flag = true;
		}
		if(str.length()<8){
			flag = true;
		}

        return flag;
	   
	}

	public String Decode(String ppPwd){
		// 密码解密
		try {
			DESCryption mmDESCryption = new DESCryption();
			ppPwd = mmDESCryption.decode(ppPwd);
			return ppPwd;
		}catch(Exception e) {				
			return ppPwd;
		}
	}

}
